package chuangyi.com.org.ViewModel;

import android.content.Context;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;

/**
 * Created by zxh on 16/12/20.
 */

public class MyFollowModel extends XListModel {
    private boolean loading;
    private int uid;
    private UserInfoModel self;

    public UserInfoModel getSelf() {
        return self;
    }

    public void setSelf(UserInfoModel self) {
        this.self = self;
    }

    public MyFollowModel(Context context, int uid) {
        super(context);
        this.uid = uid;
        if (uid == User.getCurrentUser().getUid()) {
            super.getHandler().setTitle("关注");
        } else {
            super.getHandler().setTitle("关注");
        }

        super.getAdapter().setVariableId(BR.user);
    }

    @Override
    public void load(final int page) {
        if (loading) {
            return;
        }
        setNoMore();
        loading = true;
        getAdapter().clear(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                XListAdapterInterface adapter = getAdapter();
                ArrayList list = WebApi.getMyFollow(getContext(), uid);
                try {
                    if (list == null) {
                        list = new ArrayList();
                    }
                    for (Object item : list) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        UserItemModel user = new UserItemModel(getContext(), R.layout.i_follow);
                        ArrayList jobList = (ArrayList) map.get("job");
                        if (jobList != null) {
                            for (Object job : jobList) {
                                user.setJob((String) job);
                            }
                        }
                        user.setAllowUnAttetion(self != null);
                        user.setExpert(Utils.getBooleanValue(map.get("expertflag")));
                        user.setAttented(Utils.getBooleanValue(map.get("gzflag")));
                        user.setAvatar((String) map.get("avatar"));
                        user.setCompany((String) map.get("company"));
                        user.setNickName((String) map.get("nickname"));
                        user.setUid(Utils.getIntValue(map.get("uid")));
                        user.setParent(MyFollowModel.this);
                        adapter.add(user);
                    }
                } catch (Exception ex) {
                    showToast("数据解析时发生错误");
                }
                notifyDataSetChanged(list);
                loading = false;
            }
        }).start();
    }

    @Override
    public void notifyListChanged(int action, final BindingListItem item) {
        if (action == ListBindingAdapter.ACTION_DEL) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    getAdapter().remove(item);
                    getAdapter().notifyDataSetChanged();
                }
            });
            if (action == ListBindingAdapter.ACTION_DEL) {
                if (self != null) {
                    self.setWdgz(self.getWdgz() - 1);
                }
            } else if (action == ListBindingAdapter.ACTION_ADD) {
                if (self != null) {
                    self.setWdgz(self.getWdgz() + 1);
                }
            }
        }
    }
}
